<template>
  <div class="dialog">
    <el-dialog
      :title="'设备保养计划'+titleText"
      :visible.sync="showDialog"
      width="35%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-form-item label="保养计划名称：" class="w100p" prop="planName">
          <el-input
            v-model="formData.planName"
            clearable
            placeholder="请输入保养计划名称"
          />
        </el-form-item>
        <el-form-item label="计划有效日期：" class="w100p">
          <el-form-item class="w100p" prop="startTime">
            <el-date-picker
              v-model="formData.startTime"
              type="datetime"
              placeholder="选择日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              clearable
            />
          </el-form-item>
          <el-form-item class="w100p" prop="endTime">
            <el-date-picker
              v-model="formData.endTime"
              type="datetime"
              placeholder="选择日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              clearable
            />
          </el-form-item>
        </el-form-item>

        <el-form-item label="任务生成时间：" class="w100p" prop="generateHour">
          <el-select
            v-model="formData.generateHour"
            clearable
            filterable
            placeholder="请选择任务生成时间"
          >
            <el-option
              v-for="item of mixGenerateHourList"
              :key="item.generateHourId"
              :label="item.generateHourName"
              :value="item.generateHourId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="计划启停" class="w100p">
          <el-switch
            style="height: 36px;"
            v-model="formData.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import maintenancePlanApi from "@/api/equip-maintenance-manage/maintenance-plan";
import { maintenanceMixins } from "@/utils/maintenance-mixins";
export default {
  name: "",
  components: {},
  mixins: [maintenanceMixins],
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: "",
    },
    nowChanceStau: {
      type: String,
      default: "",
    },
    show: {
      type: Boolean,
      default: false,
    },
    editObject: {
      type: [Object],
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      formData: {},
      rules: {
        planName: [
          {
            required: true,
            message: "请输入保养名称",
            trigger: ["change", "blur"],
          },
        ],
        startTime: [
          {
            required: true,
            message: "请选择计划有效日期",
            trigger: ["change", "blur"],
          },
        ],
        endTime: [
          {
            required: true,
            message: "请选择计划有效日期",
            trigger: ["change", "blur"],
          },
        ],
        generateHour: [
          {
            required: true,
            message: "请选择任务生成时间",
            trigger: ["change", "blur"],
          },
        ],
      },

      showDialog: this.show,
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === "add") {
        this.formData = {};
      }
      if (v === "edit") {
        this.formData = this.editObject;
      }
    },
  },
  created() {},
  mounted() {},
  methods: {
    validateDatetime() {
      let ret = false;
      if (this.showDatePicker) {
        let start = new Date(this.formData.startTime).getTime();
        let end = new Date(this.formData.endTime).getTime();
        ret = start <= end;
      } else {
        ret = this.formData.startTime <= this.formData.endTime;
      }
      if (!ret) {
        this.$message.error("计划有效日期的起始时间不能大于结束时间");
      }
      return ret;
    },
    save(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid && this.validateDatetime()) {
          if (this.nowChanceStau === "edit") {
            this.formData.planId = this.editObject.planId;
            maintenancePlanApi.edit(this.formData).then((res) => {
              if (res.code === 1) {
                this.$message.success("修改成功！");
                this.close();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else if (this.nowChanceStau === "add") {
            this.formData.status = true;
            this.formData.organizationMark = sessionStorage.organizationMark;
            maintenancePlanApi.add(this.formData).then((res) => {
              if (res.code === 1) {
                this.$message.success(res.msg);
                this.close();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.clearValidate();
      this.$emit("closeDialog");
    },
    cancel() {
      this.clearValidate();
      this.$emit("cancelDialog");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
